package other;

import org.apache.log4j.Logger;

import java.util.concurrent.TimeUnit;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/18
 */
public class Test3 {

    static final Logger log=Logger.getLogger(Test3.class);

    //静态方法，上类锁，函数功能为连续打印1000个value值，调用时会传1，所以会打印1000个1
    synchronized public  static void mB() throws InterruptedException {
        log.debug("类锁");
        TimeUnit.SECONDS.sleep(5);
        log.debug("5秒后继续");
    }

    public void mC() throws InterruptedException {
        //修饰this上对象锁，函数功能也是连续打印1000个value值，调用时会传2，所以会打印1000个2
        synchronized (this) {
            log.debug("对象锁");
        }
    }

    public static void main(String[] args) {
        Test3 test3 = new Test3();

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    mB();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                   test3.mC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        thread2.start();

    }
}
